package cn.itcast.search.controller;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.dto.search.SearchReqDTO;
import cn.itcast.hmall.pojo.item.ItemDoc;
import cn.itcast.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 批量向es导入数据
     *
     * @return
     */
    @GetMapping("/importItemData")
    public ResultDTO searchData() {
        return searchService.searchData();
    }

    /**
     * 搜索栏自动补全
     * @param key
     * @return
     */
    @GetMapping("/suggestion")
    public List<String> searchSuggestion(@RequestParam("key") String key){
        return searchService.searchSuggestion(key);
    }

    /**
     * 过滤项聚合功能
     * @param searchReqDTO
     * @return
     */
    @PostMapping("/filters")
    public Map<String,List<String>> searchFilters(@RequestBody SearchReqDTO searchReqDTO){
        return searchService.searchFilters(searchReqDTO);
    }

    /**
     *基本搜索功能
     * @param searchReqDTO
     * @return
     */
    @PostMapping("/list")
    public PageDTO<ItemDoc> searchList(@RequestBody SearchReqDTO searchReqDTO){
        return searchService.searchList(searchReqDTO);
    }

}
